var app = getApp()
Page({
  data: {
    // 手机号
    phone: "",
    // 验证码
    yzm: "",
    // button属性
    wdisabled: false,
    // 获取验证码
    yzmdisabled: false,
    sms_zt: '获取验证码',
    second: 60,
    nullHouse1: false,
    nullHouse2: true,
    yzmcolor: "red",
    cellPhone: ''
  },
  onLoad: function (options) {
    this.setData({
      cellPhone: options.phone
    })
    this.inputNull()
  },
  setDisabled: function () {
    wx.navigateBack();
  }, 
  // 获取手机号
  userNameInputphone: function (e) {
    this.setData({
      phone: e.detail.value
    })
    this.inputNull()
  },
  // 获取验证码
  userNameInputcode: function (e) {
    this.setData({
      yzm: e.detail.value
    })
    this.inputNull()
  },
  // 倒计时
  countNum: function () {
    var _that = this;
    if (_that.data.second == 0) {
      _that.setData({
        second: 60,
        nullHouse1: false,
        nullHouse2: true
      });
      return
    }
    var time = setTimeout(function () {
      _that.setData({
        second: _that.data.second - 1,
        nullHouse1: true,
        nullHouse2: false
      });
      _that.countNum()
    }, 1000)
  },
  // 获取验证码，调取接口
  countdown: function (e) {
    var _that = this
    app.request({
      url: app.globalData.apiUrl +'/mobileOfficing/api/sendCheckCode?mobilePhone=' + this.data.phone,
      method: "get",
      data: {
      },
      header: {
        'header': 'Content-Type: application/json',
        'sessionId': wx.getStorageSync(app.globalData.sessionId)
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.result) {
          _that.countNum();
          _that.setData({
            yzmdisabled: false,
            yzmcolor: "#2e85f5"
          })
          wx.showToast({  //提示弹窗
            title: '发送成功',
            icon: 'succes',
            duration: 3000,
            mask: true,
          })
        } else {
          _that.setData(
            { popErrorMsg: res.data.msg }
          );
          _that.ohShitfadeOut();
        }
      }
    })
  },
  // 手机号失去焦点验证
  inputblur: function (e) {
    var text = e.detail.value;
    var zephone = /^1[3|4|5|7|8][0-9]{9}$/
    var _that = this
    var phone = _that.data.phone
    if (zephone.test(text) == false) {
      _that.setData(
        {
          popErrorMsg: "手机号格式不正确", yzmdisabled: true, yzmcolor: "#dddddd"
        },
      );
      _that.ohShitfadeOut();
    }
  },
  //确定按钮置灰情况判断
  inputNull: function () {
    if (this.data.phone == "" || this.data.yzm == "") {
      this.setData({
        wdisabled: true,
      })
    } else {
      this.setData({
        wdisabled: false,
      })
    }
    if (this.data.phone == "") {
      this.setData({
        yzmdisabled: true,
        yzmcolor: "#dddddd"
      })
    } else {
      this.setData({
        yzmdisabled: false,
        yzmcolor: "#2e85f5"
      })
    }
  },
  // 确定按钮时间出发事件
  loginBtnClick: function (e) {
    var _that = this
    console.log("手机号" + this.data.phone + "验证码" + this.data.yzm);
    var yzm = _that.data.yzm
    var phone = _that.data.phone
    var zephone = /^1[3|4|5|7|8][0-9]{9}$/
    if (zephone.test(phone) == false) {
      var _that = this
      _that.setData(
        { popErrorMsg: "手机号格式不正确" },
      );
      _that.ohShitfadeOut();
      return
    } else {
      app.request({
        url: app.globalData.apiUrl +'/mobileOfficing/api/verifyMobilePhone?mobilePhone=' + this.data.phone + "&code=" + yzm,
        method: "get",
        data: {

        },
        header: {
          'header': 'Content-Type: application/json',
          'sessionId': wx.getStorageSync(app.globalData.sessionId),
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.result) {
            _that.countNum();
            _that.setData({
              yzmdisabled: false,
              yzmcolor: "#2e85f5"
            })
            wx.showToast({
              title: '更换成功',
              icon: 'succes',
              duration: 1000,
              mask: true,
              complete: function () {
                wx.navigateBack();
              },
            })
          } else {
            _that.setData(
              { popErrorMsg: res.data.msg }
            );
            _that.ohShitfadeOut();
          }
        }
      })
    }
  },
  // 弹窗关闭
  ohShitfadeOut() {
    var fadeOutTimeout = setTimeout(() => {
      this.setData({ popErrorMsg: '' });
      clearTimeout(fadeOutTimeout);
    }, 3000);
  },
});
